package src.dump;

import java.util.Iterator;
import java.util.ArrayList;
import java.util.regex.*;
import src.dump.ResourceURLServer;

/**
 **  @author honghua.he 2013-12-28 22:43:51
 */

public class StockList {

public static String stockListUrl = "http://quote.eastmoney.com/stocklist.html";
public static String stockRegex = "http://quote.eastmoney.com/(sz|sh)(.*).html";

public static ArrayList getStockList() {
    ArrayList stockList = new ArrayList();
    String xmlContent = downloadXmlContent(stockListUrl);
    if (xmlContent.length() == 0) {
        return stockList;
    }
    stockList = parseStockListFromXml(xmlContent);
    return stockList;
}

public static String downloadXmlContent(String urlStr) {
    String xmlContent = "";
    try {
        xmlContent = ResourceURLServer.getURLResource(stockListUrl);
        return xmlContent;
    } catch (Exception e) {
        e.printStackTrace();
        return xmlContent;
    }
}

public static ArrayList parseStockListFromXml(String xmlContent) {
    ArrayList stockList = new ArrayList();
    Pattern pat = Pattern.compile(stockRegex);
    Matcher m = pat.matcher(xmlContent);
    while (m.find()) {
        stockList.add(m.group(2).trim());
    }

    return stockList;
}

public static void main(String[] args) {
    ArrayList stockList = StockList.getStockList();
    for (Iterator itr = stockList.iterator(); itr.hasNext(); ) {
        System.out.println(itr.next());
    }
}

}


